package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.OwnBusiUnitAdapter;
import com.wwwarehouse.taskcenter.bean.OwnBusinessUnitBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/11 9:17
 * @描述 ${TODO}
 */

public class OwnBusinessUnitFragmentChild extends BaseFragment {

    private View        mView;
    private StateLayout mStateLayout;
    private ListView    mListView;
    private Map         mOwnBusinessChildMap;
    private int         mPage;
    ArrayList<OwnBusinessUnitBean> mCurrentList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_own_business_unit_child, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_state);
        mListView = findView(mView, R.id.lv_own_busi_unit);
    }

    private void initData() {
        mCurrentList = new ArrayList<>();
        Bundle ownBusiUnitChildBundle = getArguments();
        mOwnBusinessChildMap = new HashMap();
        if (ownBusiUnitChildBundle != null) {
            mPage = ownBusiUnitChildBundle.getInt("page");
            int inviteeId = ownBusiUnitChildBundle.getInt("inviteeId");
            mOwnBusinessChildMap.put("inviteeId", inviteeId);
        }
        getOwnBusinessUnitChild();
    }

    private void getOwnBusinessUnitChild() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(TaskCenterConstant.BUSINESS_UNITS_BY_INVITER, mOwnBusinessChildMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                if ("0".equals(commonClass.getCode())) {
                    ArrayList<OwnBusinessUnitBean> ownBusinessUnitList = (ArrayList<OwnBusinessUnitBean>) JSON.parseArray(commonClass.getData().toString(), OwnBusinessUnitBean.class);

                    if (ownBusinessUnitList.size() > 7 * mPage) {


                        for (int j = 0 + (mPage - 1) * 7; j < 7 * mPage; j++) {
                            mCurrentList.add(ownBusinessUnitList.get(j));
                        }

                    } else {
                        for (int j = 0 + 7 * (mPage - 1); j < ownBusinessUnitList.size(); j++) {
                            mCurrentList.add(ownBusinessUnitList.get(j));
                        }
                    }

                    OwnBusiUnitAdapter ownBusiUnitAdapter = new OwnBusiUnitAdapter(mActivity, mCurrentList, mListView);
                    mListView.setAdapter(ownBusiUnitAdapter);
                } else {
                    mStateLayout.showSystemView(commonClass.getMsg(), true);
                    mStateLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getOwnBusinessUnitChild();
                        }
                    });
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getOwnBusinessUnitChild();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    @Override
    public void setTitle() {
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
